package io.renren.common.utils.image;


import io.renren.modules.nuaa.entity.AllContentEntity;
import io.renren.modules.nuaa.entity.ContentEntity;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

/**
 * 测试类
 * author: 峰影
 */
public class ImageTool {


    public static void main(String[] args) throws Exception {
        // 获取系统字体
        getFonts();
        // 组装数据
        /*String no = "EDECO-2021-0001";
        String realName = "张三";
        String item = "XXXXXXXXXXX赛";
        String group = "小组赛";
        String grade = "一等奖";
        String company = "青岛一笔一画信息科技有限公司";
        String teacher = "王老师";
        String ercode = "http://img.fii-cc.com/fii/20230601/0bf065a9ab1d4b558c305262f235a9ca.png";
        AllContentEntity all = rongyu(no,realName,item,group,grade,company,teacher,ercode);
        // 遍历数据并画图片
        drawList(all);*/

        // 领队
        /*String area = "（山东省潍坊地区选拔赛）";
        String userName = "张三";
        String no = "沪A-100101";
        String teamName = "上海市青浦区实验小学代表队";
        String photo = "http://img.fii-cc.com/fii/20230601/0bf065a9ab1d4b558c305262f235a9ca.png";

        AllContentEntity all = lingdui(area, userName, no, teamName, photo);
        drawList(all);*/

        // 选手
        /*String area = "（山东省潍坊地区选拔赛）";
        String userName = "张三";
        String no = "沪A-100101";
        String teamName = "上海市青浦区实验小学代表队";
        String photo = "http://img.fii-cc.com/fii/20230601/0bf065a9ab1d4b558c305262f235a9ca.png";
        List<String> list = new ArrayList<>();
        list.add("机甲大师空地协同对抗赛");
        AllContentEntity all = xuanshou(area, userName, no ,"小学组", teamName, photo, list);
        drawList(all);*/

        String no = "EDE07A10003T99700140";
        String bgPic = "/Users/wanghucheng/Downloads/laoshi/"+no+".png";
        String ercode = QrCodeCreateUtil.genGR("http://login.int-ede.com/renren-admin/front/spuser/tr/" + no);
        AllContentEntity all = rongyu_linshi(no ,ercode, bgPic);
        drawList(all);
    }

    // ture:windows系统字体
    final static boolean isWin = true;
    final static String MiSansWin =  isWin ? "MiSans" : "MiSans-Bold";
    final static String MiSansDemiboldWin = isWin ? "MiSans Demibold" : "MiSans-Demibold";
    final static String MiSansMedium = isWin ? "MiSans Medium" : "MiSans-Medium";
    final static String MiSansNormal = isWin ? "MiSans Normal" : "MiSans-Normal";


    public static AllContentEntity rongyu_linshi(String no, String ercode , String bgPic) {
        // 编号 姓名 赛项 小组  奖等  单位  老师  二维码
        List<ContentEntity> list = new ArrayList<ContentEntity>();
        final int width = 2481;
        final int height = 3508;

        // 305,2918
        ContentEntity content7 = new ContentEntity(true, 305f / width, 2918f / height);
        content7.setContent(ercode);
        content7.setImgWidth(240);
        content7.setImgHeight(240);
        list.add(content7);

        AllContentEntity all = new AllContentEntity();
        all.setSourcePath(bgPic);
        all.setContentList(list);
        return all;
    }


    /**
     * 奖证-选手
     *
     * @return void
     * @Author 峰影
     */
    public static AllContentEntity rongyu(String no, String realName, String item, String group, String grade,
                                          String company, String teacher, String ercode , String bgPic) {
        getFonts();
        // 编号 姓名 赛项 小组  奖等  单位  老师  二维码
        List<ContentEntity> list = new ArrayList<ContentEntity>();
        final int width = 2481;
        final int height = 3508;

        //1147,323  MiSansMedium
        ContentEntity content1 = new ContentEntity(no, "微软雅黑",
                0, 10 * 4, 1660f / width, 363f / height);
        content1.setFontSpace(29);
        list.add(content1);

        // 三个字默认位置
        float realNameLeft = 462f / width;
        // 两个字位置
        if(realName.length()==2){
            realNameLeft = 508f / width;
        }
        // 四个字默认位置
        if(realName.length()==4){
            realNameLeft = 430f / width;
        }
        ContentEntity content2 = new ContentEntity(realName, MiSansDemiboldWin,
                0, 18 * 4, realNameLeft, 1982f / height);
        content2.setFontSpace(18 * 4);
        list.add(content2);

        //计算位置：文字显示在正中间位置
        String item2 = item + "（"+group+"）";
        float areaX = (width - item2.length() * 18 * 4) / 2;
        // 808,2168
        ContentEntity content3 = new ContentEntity(item2, MiSansDemiboldWin,
                1, 18 * 4, areaX / width, 2240f / height);
        content3.setFontSpace(18 * 4);
        list.add(content3);

        // 814,2320
        //计算位置：文字显示在正中间位置
        float areaGradeX = (width - grade.length() * 70 * 4) / 2;
        ContentEntity content4 = new ContentEntity(grade, "隶书-Regular",
                0, 62 * 4, areaGradeX / width, 2510f / height);
        content4.setFontColor(new Color(255, 0, 0));
        content4.setFontSpace(70 * 4);
        list.add(content4);

        // 809,2946
        ContentEntity content5 = new ContentEntity(company, MiSansNormal,
                0, 11 * 4, 809f / width, 2986f / height);
        content5.setFontSpace(11 * 4);
        list.add(content5);

        //807,3015
        ContentEntity content6 = new ContentEntity(teacher, MiSansNormal,
                0, 11 * 4, 809f / width, 3060f / height);
        content6.setFontColor(new Color(25, 25, 25));
        content6.setFontSpace(11 * 4);
        list.add(content6);

        // 305,2918
        ContentEntity content7 = new ContentEntity(true, 305f / width, 2918f / height);
        content7.setContent(ercode);
        content7.setImgWidth(240);
        content7.setImgHeight(240);
        list.add(content7);

        AllContentEntity all = new AllContentEntity();
        all.setSourcePath(bgPic);

        //all.setTargetPath("/Users/wanghucheng/Downloads/test5/rongyu.jpg");
        all.setContentList(list);

        return all;
    }

    /**
     * 奖证-指导老师
     * @param no 序号
     * @param realName 姓名
     * @param item  赛项类型+赛项
     * @param ercode  二维码
     * @param bgPic  背景
     * @return
     */
    public static AllContentEntity rongyuTeacher(String no, String realName, String item, String ercode , String bgPic) {
        //getFonts();
        // 编号 姓名 赛项 小组  奖等  单位  老师  二维码
        List<ContentEntity> list = new ArrayList<ContentEntity>();
        final int width = 2481;
        final int height = 3508;

        //1147,323  MiSansMedium
        ContentEntity content1 = new ContentEntity(no, "微软雅黑",
                0, 10 * 4, 1660f / width, 363f / height);
        content1.setFontSpace(29);
        list.add(content1);

        // 三个字默认位置
        float realNameLeft = 569f / width;
        // 两个字位置
        if(realName.length()==2){
            realNameLeft = 605f / width;
        }
        // 四个字默认位置
        if(realName.length()==4){
            realNameLeft = 533f / width;
        }
        ContentEntity content2 = new ContentEntity(realName, MiSansDemiboldWin,
                0, 18 * 4, realNameLeft, 2173f / height);
        content2.setFontSpace(18 * 4);
        list.add(content2);

        // 814,2320
        //计算位置：文字显示在正中间位置
        float areaGradeX = (width - item.length() * 18 * 4) / 2;
        ContentEntity content4 = new ContentEntity(item, MiSansDemiboldWin,
                0, 18 * 4, areaGradeX / width, 2330f / height);
        content4.setFontSpace(18 * 4);
        list.add(content4);

        // 305,2918
        ContentEntity content7 = new ContentEntity(true, 305f / width, 2918f / height);
        content7.setContent(ercode);
        content7.setImgWidth(240);
        content7.setImgHeight(240);
        list.add(content7);

        AllContentEntity all = new AllContentEntity();
        all.setSourcePath(bgPic);

        //all.setTargetPath("/Users/wanghucheng/Downloads/test5/rongyu.jpg");
        all.setContentList(list);

        return all;
    }


    /**
     * 领队证
     * 4处文字：地区、姓名、证件号、代表队
     * 1处图片：一寸照片
     *
     * @return void
     * @Author 峰影
     */
    public static AllContentEntity lingdui(String area, String userName, String no, String teamName, String company,
                                           String photo, String bgPic) {
        final int width = 1005;
        final int height = 1478;
        List<ContentEntity> list = new ArrayList<ContentEntity>();
        //计算位置：文字显示在正中间位置  312
        /*float areaX = (width - area.length() * 8 * 4) / 2;
        ContentEntity content0 = new ContentEntity(area, MiSansWin,
                1, 8 * 4, areaX / width, 454f / height);
        content0.setFontColor(new Color(255, 255, 255));
        list.add(content0);*/

        int userNameSize = 24;
        int userNameSpace = 28;
        int userNameLength = userName.contains("·") ? userName.length() -1 : userName.length();
        //6-10个字使用16号字体
        if(userNameLength >=6 && userNameLength <= 8){
            userNameSize = 16;
            userNameSpace = 16;
        }
        if(userNameLength > 8){
            userNameSize = 10;
            userNameSpace = 10;
        }
        ContentEntity content1 = new ContentEntity(userName, MiSansWin,
                1, userNameSize * 4, 135f / width, 896f / height);
        content1.setFontColor(new Color(255, 255, 255));
        content1.setFontSpace(userNameSpace * 4);
        list.add(content1);

        ContentEntity content2 = new ContentEntity(no, MiSansDemiboldWin,
                1, 8 * 4, 135f / width, 1022f / height);
        content2.setFontColor(new Color(255, 255, 255));
        content2.setFontSpace(8 * 4);
        list.add(content2);

        ContentEntity content3 = new ContentEntity(teamName+"（领队）", MiSansDemiboldWin,
                1, 8 * 4, 135f / width, 972f / height);
        content3.setFontColor(new Color(255, 255, 255));
        content3.setFontSpace(8 * 4);
        list.add(content3);

        ContentEntity content4 = new ContentEntity(company, MiSansDemiboldWin,
                1, 8 * 4, 135f / width, 1194f / height);
        content4.setFontColor(new Color(255, 255, 255));
        content4.setFontSpace(8 * 4);
        list.add(content4);

        //373 1213
        //250 350
        //125 175
        ContentEntity content8 = new ContentEntity(true, 135f / width, 250f / height);
        content8.setContent(photo);
        content8.setImgWidth(360);
        content8.setImgHeight(500);
        list.add(content8);

        AllContentEntity all = new AllContentEntity();
        //带文字
        //all.setSourcePath("http://img.fii-cc.com/fii/20230531/6f781175caba4c8cbad026b1f23aa531.jpg");
        //不带文字
        //all.setSourcePath("http://img.fii-cc.com/fii/20230626/d49e97fb3cd24dc5ab3881c30e964285.jpg");
        all.setSourcePath(bgPic);

        //all.setTargetPath("/Users/wanghucheng/Downloads/test5/lingdui.jpg");
        all.setContentList(list);

        return all;
    }

    /**
     * 选手证
     * 6处文字：地区、姓名、证件号、代表队、参赛项目、项目明细
     * 1处图片：一寸照片
     * @param area 地区
     * @param userName 姓名
     * @param no 证件号
     * @param groupName 代表队
     * @param teamName 参赛项目
     * @param photo 一寸照片
     * @param items 项目明细
     * @return
     */
    public static AllContentEntity xuanshou(String area, String userName, String no, String groupName, String teamName,
                                            String photo, List<String> items, String bgPic) {
        getFonts();
        final int width = 1005;
        final int height = 1477;
        List<ContentEntity> list = new ArrayList<ContentEntity>();
        //计算位置：文字显示在正中间位置  312
        /*float areaX = (width - area.length() * 8 * 4) / 2;
        ContentEntity content0 = new ContentEntity(area, MiSansWin,
                1, 8 * 4, areaX / width, 454f / height);
        content0.setFontColor(new Color(255, 255, 255));
        list.add(content0);*/

        int userNameSize = 24;
        int userNameSpace = 28;
        int userNameLength = userName.contains("·") ? userName.length() -1 : userName.length();
        //6-10个字使用16号字体
        if(userNameLength >=6 && userNameLength <= 8){
            userNameSize = 16;
            userNameSpace = 16;
        }
        if(userNameLength > 8){
            userNameSize = 10;
            userNameSpace = 10;
        }
        ContentEntity content1 = new ContentEntity(userName, MiSansWin,
                1, userNameSize * 4, 180f / width, 896f / height);
        content1.setFontColor(new Color(255, 255, 255));
        content1.setFontSpace(userNameSpace * 4);
        list.add(content1);

        ContentEntity content2 = new ContentEntity(groupName+"/"+no, MiSansDemiboldWin,
                1, 9 * 4, 180f / width, 971f / height);
        content2.setFontColor(new Color(255, 255, 255));
        content2.setFontSpace(9 * 4);
        list.add(content2);

        //判断是否有括号（ 替换成成 『
        if (teamName.contains("（")) {
            teamName = teamName.replace("（", "︵");
        }
        if (teamName.contains("）")) {
            teamName = teamName.replace("）", "︶");
        }
        if (teamName.contains("(")) {
            teamName = teamName.replace("(", "︵");
        }
        if (teamName.contains(")")) {
            teamName = teamName.replace(")", "︶");
        }
        ContentEntity content3 = new ContentEntity(teamName, MiSansDemiboldWin,
                1, 8 * 4, 80f / width, 160f / height, 1);
        content3.setFontColor(new Color(255, 255, 255));
        content3.setFontSpace(9 * 4);
        list.add(content3);

        /*ContentEntity content4 = new ContentEntity("参赛项目", MiSansWin,
                1, 10 * 4, 423f / width, 1140f / height);
        content4.setFontColor(new Color(255, 255, 255));
        content4.setFontSpace(10 * 4);
        list.add(content4);*/

        //参赛项目明细
        for (int i = 0; i < items.size(); i++) {
            String item = items.get(i);
            Integer fontSize = 12 * 4;
            if(item.contains("空中侦察赛（第一视角）单机") || item.contains("空中侦察赛（第一视角）双机")
                    || item.contains("空中侦察赛（第一视角）三机") || item.contains("动力飞行器创意赛（设计赛）")
                    || item.contains("动力飞行器创意赛（竞速赛）") ){
                fontSize = 11 * 4;
            }
            ContentEntity content5 = new ContentEntity(items.get(i), MiSansDemiboldWin,
                    1, fontSize, 180f / width, (1115f + (i + 1) * 60) / height);
            content5.setFontColor(new Color(255, 255, 255));
            content5.setFontSpace(fontSize);
            list.add(content5);
        }

        // 证件照
        ContentEntity content8 = new ContentEntity(true, 180f / width, 250f / height);
        content8.setContent(photo);
        content8.setImgWidth(360);
        content8.setImgHeight(500);
        list.add(content8);

        AllContentEntity all = new AllContentEntity();
        //不带背景
        //all.setSourcePath("http://img.fii-cc.com/fii/20230626/72c3cf51121144c8a0b73e29dcd54e07.jpg");
        //all.setTargetPath("/Users/wanghucheng/Downloads/test5/xuanshou.jpg");
        all.setSourcePath(bgPic);
        all.setContentList(list);

        return all;
    }


    /**
     * 生成号码布
     */
    public static AllContentEntity haomabu(String teamName, String no, String userName, String bgPic){
        final int width = 2835;
        final int height = 2363;
        List<ContentEntity> list = new ArrayList<ContentEntity>();

        //计算位置：文字显示在正中间位置
        float areaX = (width - teamName.length() * 26 * 4) / 2;
        ContentEntity content1 = new ContentEntity(teamName, MiSansWin,
                1, 26 * 4, areaX / width, 1141f / height);
        content1.setFontColor(new Color(0, 0, 0));
        content1.setFontSpace(26 * 4);
        list.add(content1);

        //582,1231
        ContentEntity content2 = new ContentEntity(no, MiSansWin,
                1, 70 * 4, 320f / width, 1511f / height);
        content2.setFontColor(new Color(0, 0, 0));
        content2.setFontSpace(70 * 4);
        list.add(content2);

        //1058,1595
        int userNameSize = 60;
        int userNameSpace = 60;
        int userNameLength = userName.contains("·") ? userName.length() -1 : userName.length();
        if(userNameLength >= 10){
            userNameSize = 45;
            userNameSpace = 45;
        }
        float areaX3 = (width - userName.length() * userNameSize * 4) / 2;
        ContentEntity content3 = new ContentEntity(userName, MiSansWin,
                1, userNameSize * 4, areaX3 / width, 1835f / height);
        content3.setFontColor(new Color(0, 0, 0));
        content3.setFontSpace(userNameSpace * 4);
        list.add(content3);

        AllContentEntity all = new AllContentEntity();
        //不带背景
        //all.setSourcePath("http://img.fii-cc.com/fii/20230626/72c3cf51121144c8a0b73e29dcd54e07.jpg");
        //all.setTargetPath("/Users/wanghucheng/Downloads/test5/xuanshou.jpg");
        all.setSourcePath(bgPic);
        all.setContentList(list);
        return all;
    }

    /**
     * 开始填充
     *
     * @param all 填充对象
     * @return void
     * @Description 开始填充
     * @Author 峰影
     */
    public static String drawList(AllContentEntity all)  {
        String url = "";
        try {
            BufferedImage image = JpegReader.readImage(JpegReader.getImgInputByUrl(all.getSourcePath()));
            for (ContentEntity item : all.getContentList()) {
                DrawImageUtil.drawContentEntity(image, item);
            }
            url = DrawImageUtil.saveLossLessImage(image, all.getTargetPath(), ImageType.IMAGE_TYPE_JPG);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }


    /**
     * 获取系统字体
     *
     * @return Font[]
     * @Description 获取系统字体
     * @Author 峰影
     */
    private static Font[] getFonts() {
        Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
        for (Font f : fonts) {
            System.out.println("Name:" + f.getFontName());
        }
        return fonts;
    }
}
